import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../page/home/index.vue";
import Register from "@/page/person/register";
import Category from "@/page/category/category";
import Cart from "@/page/cart/cart.vue";
Vue.use(VueRouter);
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
VueRouter.prototype.push = function (location, resolve = () => {}, reject = () => {}) {
  return originPush.call(this, location, resolve, reject);
};
VueRouter.prototype.replace = function (location, resolve = () => {}, reject = () => {}) {
  return originReplace.call(this, location, resolve, reject);
};
let sphRouter = new VueRouter({
  mode: "history",
  routes: [
    {
      path: "/home",
      component: Home,
      name: "Home",
      meta: {
        show: true,
      },
    },
    {
      path: "/center",
      component: () => import("@/page/center"),
    },
    {
      path: "/person",
      name: "person",
      component: () => import("@/page/person"),
      children: [
        {
          path: "center",
          component: () => import("@/page/person/center"),
        },
        {
          path: "login",
          component: () => import("@/page/person/login"),
        },
        {
          path: "register",
          component: Register,
        },
      ],
      redirect: "person/center",
    },
    {
      path: "/category",
      component: Category,
      name: "Category",
      meta: {
        show: true,
      },
    },
    {
      path: "/cart",
      component: Cart,
      name: "Cart",
      meta: {
        show: true,
      },
    },
    //重定向
    {
      path: "/",
      redirect: "/center",
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    return {
      y: 0,
    };
  },
});
// let whiteList = ["/trade", "/pay", "/pay-success", "/center", "/center/my", "/center/other"];
// sphRouter.beforeEach(async (to, from, next) => {
//   let hasToken = store.state.user.token;
//   let hasNickName = store.state.user.name;
//   //用户登陆了
//   if (hasToken) {
//     //如果登录了,还想去登录页->跳转到home
//     if (to.path == "/login" || to.path == "/register") {
//       next("/");
//     } else {
//       //登录成功:想去home|注册|search|detail|shopcart
//       //假如登陆了:想去home|注册|search|detail|shopcart->还有用户名字->放行
//       if (hasNickName) {
//         next();
//       } else {
//         try {
//           //假如登陆了:想去home|注册|search|detail|shopcart->没有用户名自发请求获取到->放行
//           await store.dispatch("user/fetchUserInfo");
//           next();
//         } catch (error) {
//           //token获取获取不到用户信息:怎么处理
//           await store.dispatch("user/logout");
//           next("/login");
//         }
//       }
//     }
//   } else {
//     //用户未登录----放行 ||未登录的情况暂时不考虑
//     if (whiteList.includes(to.path)) {
//       console.log(to.path);

//       to.path === "/center/my" ? next("/login?path=/center'") : next("/login");
//     } else {
//       next();
//     }
//   }
// });
export default sphRouter;
